/***
 *
 * 一个用于管理相机视角的类 CamerMark.js
 *
 * Daniel
 *
 * 2018.03.14
 *
 * **/
define(["objmodel"],function(objmodel){
    
    var CameraMark = function () {
    
        this.viewDefault = [
            '{"metadata":{"version":4.5,"type":"Object","generator":"Object3D.toJSON"},"object":{"uuid":"6DFD021F-9AA9-47E5-A7A9-88D7AC3CA1B1","type":"PerspectiveCamera","name":"camera","matrix":[0.5280678506503685,6.938893903907228e-18,0.8492021815265786,0,0.043494465122359625,0.9986874952275746,-0.02704659645487853,0,-0.8480875996105709,0.05121803272357506,0.5273747590762254,0,-3079.4739226622023,957.5855602928924,1560.5066491497032,1],"children":[{"uuid":"1990BFA0-36CB-47DB-962B-27C187F57385","type":"Object3D","matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}],"fov":75,"zoom":1,"near":1,"far":20000,"focus":10,"aspect":1.9712525667351128,"filmGauge":35,"filmOffset":0}}'
        ];
        this.numberData = "1";
        this.DataCache = {};
        this.data = {};
        this.cacheData = {};
        this.contArray = {
            pickArray: [],
                oldColor: [],
                id: [],
                time: null,
                otherData: [],
                oneArray:[],
                oneOldColor:[]
        
        };
        this.camera = {};
        this.viewCamera = {};
        this.world = {};
        this.parse = objmodel.parse;
        this.originalFreeP = {};                   //原始值位置
        this.originalOrbitP = {};                   //原始值位置
        
    };
    
        CameraMark.prototype.init = function (world,viewDefault,cameraOriginalP) {
        var cameraValueA = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
        var cameraValueB = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
        this.viewDefault = viewDefault || this.viewDefault;
        this.cameraOriginalP = cameraOriginalP;
        this.numberData =  world.numberData;
        this.world = world;
        this.camera = world.camera;
        this.viewCamera = world.viewCamera;
        this.originalFreeP = cameraValueA.copy(this.viewCamera).clone();
        this.originalOrbitP = cameraValueB.copy(this.camera).clone();
        this.originalOrbitP.children[0] = new THREE.Object3D();
            world.camera.originalOrbitP = this.originalOrbitP.toJSON();
            world.viewCamera.originalFreeP = this.originalFreeP.toJSON();
    };
    
    
    //保存视角书签
    CameraMark.prototype.saveMark = function (){
        var scope = this;
        return function (names,callback) {
            var mark, cameraData, cameraNow, data, cameraObjct, label;
            mark = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
            label = names || "mark1";
            cameraNow = scope.world.cameraCoupling(scope.numberData);
            mark.copy(cameraNow);
            cameraData = mark.toJSON();
            data = JSON.stringify(cameraData);
            cameraObjct = {
                text: data,
                name: label
            };
            scope.world.screenShot(function(url){
                cameraObjct.image=url;
                callback&&callback(cameraObjct);
            });
        };
       
    };
    
    //恢复视角
    CameraMark.prototype.initView = function (){
        var scope = this;
        return function () {
            var cameraP = scope.world.cameraCoupling(scope.numberData);
            if(cameraP.originalFreeP){
                cameraP.copy(scope.originalFreeP);
            }else{
                if(cameraP.originalOrbitP){
                    cameraP.copy(scope.originalOrbitP);
                    cameraP.orbitControls.target.set(0,0,0);
                }
            }
        };
    };
    
    //解析原有书签
    CameraMark.prototype.parseMark = function(){
        var scope = this;
        return function (JSONtexts) {
            var json = JSON.parse(JSONtexts);
            var cameraData = scope.parse(json);
            var markCameras = cameraData.clone();
                markCameras.children[0] = new THREE.Object3D();
            var newCamera = scope.world.cameraCoupling(scope.numberData);
            newCamera.copy(markCameras);
        };
    };
    
    //返回视角书签(离线操作)
    CameraMark.prototype.cameraMark = function (){
        
        var scope = this;
        var jsonDa, cameraDa, json, newCamera, markCameras, label;
        
        return function (sub, text, number) {
            
            var flag = text;
            if (flag !== undefined){
                label = scope.viewDefault;
                var potext = label[number];
                jsonDa = potext;
                callback(potext);
                return;
            }else{
                label = sub;
                scope.DataCache.get(label, callback);
                return
            }
            
            function callback(dataText){
                if (flag)
                {
                    json = JSON.parse(jsonDa);
                } else
                {
                    jsonDa = dataText.data[label];
                    json = JSON.parse(jsonDa.text);
                }
        
                cameraDa = scope.objmodel.parse(json);
                markCameras = cameraDa.clone();
                newCamera = scope.world.cameraCoupling(scope.numberData);
                newCamera.copy(markCameras);
            }
            
        };
        
    };
    
    //删除视角书签
    function deleteMark(names){
        if (names)
        {
        
        }
    }
    
    //初始化缓存(离线操作)
    CameraMark.prototype.initCacheData = function (Boolean){
        THREE.Cache.enabled = Boolean;
        var scope = this;
        this.DataCache = this.cacheData.init({
            dataDB: scope.data,
            number: 1,
            callback: function (){
                var datacache = scope.DataCache;
                setTimeout(function (){
                    datacache.init(function (prase){
                        if (prase.length > 0)
                        {
                            scope.contArray.otherData = prase[0].data;
                            var marktree = scope.contArray.otherData;
                            scope.viewMarkTree = Object.keys(marktree);
                        }
                        
                    });
                    
                }, 300);
            }
        });
        
        
    };
    
    
    //单独加载视角书签
    
    // function saveJsonData(urlArray){
    //     if(urlArray){
    //         var len = urlArray.length;
    //         for(var i = 0; i < urlArray.length; i++){
    //             var objects = {
    //                 url: urlArray[i],
    //                 data:null,
    //                 id: i
    //             }
    //             var load = Ajax.init(objects,function (texts) {
    //                 debugger;
    //                 if(texts){
    //                     var  dbObject = {
    //                         name: "jsonData",
    //                         id: texts.id,
    //                         datas: texts.data
    //                     }
    //                     DataCache.add(dbObject);
    //                 }
    //
    //             });
    //
    //         }
    //
    //     }
    //
    // }
    
    
    return CameraMark;
    
    
    
});